home *** CD-ROM | disk | FTP | other *** search
/ Netscape Plug-Ins Developer's Kit / Netscape_Plug-Ins_Developers_Kit.iso / CGIPERL / MACPERL / MSRCE418.HQX / Perl Source ƒ / Perl / missing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  6.1 KB  |  341 lines

  1. /* $Header: tyrathect:Development:Perl::RCS:missing.c,v 1.2 1994/05/04 02:12:43 neeri Exp $
  2.  *
  3.  *    Copyright (c) 1991-1994 Matthias Neeracher
  4.  *
  5.  *    You may distribute under the terms of the Perl Artistic License,
  6.  *    as specified in the README file.
  7.  *
  8.  * $Log: missing.c,v $
  9.  * Revision 1.2  1994/05/04  02:12:43  neeri
  10.  * Added gmtime(), enabled utime(), reduced spinning.
  11.  *
  12.  * Revision 1.1  1994/03/01  23:38:17  neeri
  13.  * Initial revision
  14.  *
  15.  */
  16.  
  17. #define RESOLVE_MAC_CONFLICTS
  18.  
  19. #include "EXTERN.h"
  20. #include "perl.h"
  21.  
  22. #include <time.h>
  23.  
  24. #include <Folders.h>
  25. #include <Events.h>
  26. #include <OSUtils.h>
  27. #include <LowMem.h>
  28. #include <GUSI.h>
  29. #include <fp.h>
  30.  
  31. #if !defined(powerc) && !defined(__powerc)
  32. /* Allocate more stdio buffers */
  33.  
  34. FILE _iob[64] = {
  35.     0, nil, nil, nil, 0, _IOREAD,         0,
  36.     0, nil, nil, nil, 0, _IOWRT,          1,
  37.     0, nil, nil, nil, 0, _IOWRT+_IOLBF,    2
  38. };
  39.     
  40. FILE * _lastbuf = _iob + 64;
  41. #endif
  42.  
  43. /* Calls that don't exist on the mac */
  44.  
  45. /* Borrowed from msdos.c
  46.  * Just pretend that everyone is a superuser
  47.  */
  48. #define ROOT_UID    0
  49. #define ROOT_GID    0
  50. int
  51. getuid(void)
  52. {
  53.     return ROOT_UID;
  54. }
  55.  
  56. int
  57. geteuid(void)
  58. {
  59.     return ROOT_UID;
  60. }
  61.  
  62. int
  63. getgid(void)
  64. {
  65.     return ROOT_GID;
  66. }
  67.  
  68. int
  69. getegid(void)
  70. {
  71.     return ROOT_GID;
  72. }
  73.  
  74. int
  75. setuid(int uid)
  76.     return (uid==ROOT_UID?0:-1);
  77. }
  78.  
  79. int
  80. setgid(int gid)
  81.     return (gid==ROOT_GID?0:-1); 
  82. }
  83.  
  84. execv()
  85. {
  86.     fatal("execv() not implemented on the Macintosh");
  87. }
  88.  
  89. execvp()
  90. {
  91.     fatal("execvp() not implemented on the Macintosh");
  92. }
  93.  
  94. kill()
  95. {
  96.     fatal("kill() not implemented on the Macintosh");
  97. }
  98.  
  99. sleep(int seconds)
  100. {
  101.     long    ticks    =    TickCount() + seconds*60;
  102.     
  103.     while (TickCount() < ticks)
  104.         SpinMacCursor();
  105. }
  106.  
  107. do_aspawn()
  108. {
  109.     fatal("do_aspawn() not implemented on the Macintosh");
  110. }
  111.  
  112. do_spawn()
  113. {
  114.     fatal("do_spawn() not implemented on the Macintosh");
  115. }
  116.  
  117. char **environ;
  118. extern int StandAlone;
  119.  
  120. char ** init_env(char ** env)
  121. {
  122.     int        envcnt    =    0;
  123.     int        envsize    =    0;
  124.     int        varlen;
  125.     char *    envpool;
  126.     FILE *    envfile    =    0;
  127.     
  128.     for (envcnt = 0; env[envcnt]; envcnt++)    {
  129.         varlen    = strlen(env[envcnt]);
  130.         envsize    +=    varlen+strlen(env[envcnt]+varlen+1)+2;
  131.     }
  132.     
  133.     environ = (char **)     malloc((envcnt+1)*sizeof(char *));
  134.     envpool = (char *)     malloc(envsize);
  135.     
  136.     for (envcnt = 0; env[envcnt]; envcnt++)    {
  137.         environ[envcnt]     = envpool;
  138.         varlen                = strlen(env[envcnt]);
  139.         strcpy(envpool, env[envcnt]);
  140.         envpool              += varlen+1;
  141.         envpool[-1]            = '=';
  142.         strcpy(envpool, env[envcnt]+varlen+1);
  143.         envpool              += strlen(env[envcnt]+varlen+1)+1;
  144.     }
  145.  
  146.     environ[envcnt] = 0;
  147.     
  148.     return environ;
  149. }    
  150.  
  151. typedef struct PD {
  152.     struct PD *    next;
  153.     FILE *        tempFile;
  154.     FSSpec        pipeFile;
  155.     char *        execute;
  156.     long            status;
  157. } PipeDescr, *PipeDescrPtr;
  158.  
  159. static PipeDescrPtr    pipes        =    nil;
  160. static Boolean            sweeper    =    false;
  161.  
  162. void sweep()
  163. {
  164.     while (pipes)
  165.         mypclose(pipes->tempFile);
  166.     sweeper = false;
  167. }
  168.  
  169. FILE * mypopen(char * command, char * mode)
  170. {
  171.     PipeDescrPtr    pipe;
  172.     
  173.     if (!strcmp(command, "-"))
  174.         fatal("Implicit fork() on a Mac? No forking chance");
  175.         
  176.     New(666, pipe, 1, PipeDescr);
  177.     
  178.     if (!pipe)
  179.         return NULL;
  180.         
  181.     if (FSpMakeTempFile(&pipe->pipeFile))
  182.         goto failed;
  183.     pipe->execute    =    nil;
  184.     
  185.     switch(*mode)    {
  186.     case 'r':
  187.         /* Ugh ! A hardcoded command  ! */
  188.         
  189.         if (!strcmp(command, "pwd") || !strcmp(command, "directory") || !strcmp(command, "Directory")) {
  190.             char curdir[256];
  191.             
  192.             if (!(pipe->tempFile    = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
  193.                 goto delete;
  194.             if (!getcwd(curdir, 256)) 
  195.                 goto delete;
  196.             
  197.             fprintf(pipe->tempFile, "%s\n", curdir);
  198.             fclose(pipe->tempFile);
  199.         } else if (!strcmp(command, "hostname")) {
  200.             char curhostname[256];
  201.             
  202.             if (!(pipe->tempFile    = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
  203.                 goto delete;
  204.             if (gethostname(curhostname, 256)) 
  205.                 goto delete;
  206.             
  207.             fprintf(pipe->tempFile, "%s\n", curhostname);
  208.             fclose(pipe->tempFile);
  209.         } else if (SubLaunch(command, nil, &pipe->pipeFile, &pipe->pipeFile, &pipe->status))
  210.             goto delete;
  211.             
  212.         if (!(pipe->tempFile    = fopen(FSp2FullPath(&pipe->pipeFile), "r")))
  213.             goto delete;
  214.         break;
  215.     case 'w':
  216.         New(667, pipe->execute, strlen(command)+1, char);
  217.         if (!pipe->execute || !(pipe->tempFile    = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
  218.             goto delete;
  219.         strcpy(pipe->execute, command);
  220.         break;
  221.     }
  222.     
  223.     pipe->next    =    pipes;
  224.     pipes            =    pipe;
  225.     
  226.     if (!sweeper)    {
  227.         atexit(sweep);
  228.         sweeper    =    true;
  229.     }
  230.  
  231.     return pipe->tempFile;
  232. delete:
  233.     if (pipe->execute)
  234.         Safefree(pipe->execute);
  235.     HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
  236. failed:
  237.     Safefree(pipe);
  238.     
  239.     return NULL;
  240. }
  241.  
  242. int mypclose(FILE *    f)
  243. {
  244.     OSErr                err;
  245.     PipeDescrPtr *    prev;
  246.     PipeDescrPtr    pipe;
  247.     
  248.     for (prev = (PipeDescrPtr *) &pipes; pipe = *prev; prev = &pipe->next)
  249.         if (pipe->tempFile == f)
  250.             break;
  251.     
  252.     if (!pipe)
  253.         return -1;
  254.     
  255.     *prev = pipe->next;
  256.     
  257.     fclose(f);
  258.     
  259.     if (pipe->execute)
  260.         err = SubLaunch(pipe->execute, &pipe->pipeFile, nil, nil, &pipe->status);
  261.     else
  262.         err = noErr;
  263.         
  264.     HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
  265.     if (pipe->execute)
  266.         Safefree(pipe->execute);
  267.     Safefree(pipe);
  268.     
  269.     return err? -1 : (int) pipe->status;    
  270. }
  271.  
  272. void SpinMacCursor()
  273. {
  274.     static long lastSpin = 0;
  275.     
  276.     if (LMGetTicks() - lastSpin < 1)
  277.         return;
  278.         
  279.     lastSpin = LMGetTicks();
  280.         
  281.     (GUSIGetSpin())(SP_AUTO_SPIN, 1);
  282. }
  283.  
  284. struct tm * gmtime(const time_t * timer)
  285. {
  286.     MachineLocation     loc;
  287.     struct tm *            swatch;
  288.     long                    delta;
  289.     time_t                rolex;
  290.     
  291.     ReadLocation(&loc);
  292.     
  293.     if (!loc.latitude && !loc.longitude)
  294.         return localtime(timer);    /* This is incorrect unless you live in Greenwich */
  295.     
  296.     delta = loc.u.gmtDelta & 0xFFFFFF;
  297.     
  298.     if (delta & 0x800000)
  299.         delta = (long) ((unsigned long) delta | 0xFF000000);
  300.         
  301.     rolex = (unsigned long) ((long) *timer - delta);
  302.     
  303.     swatch = localtime(&rolex);
  304.     
  305.     swatch->tm_isdst = (loc.u.dlsDelta & 0x80) != 0;
  306.     
  307.     return swatch;
  308.  }
  309.  
  310. void mac_times(struct tms * t)
  311. {
  312.     t->tms_utime = clock() - gStartClock;
  313.     t->tms_stime = 0;
  314.     t->tms_cutime = 0;
  315.     t->tms_cstime = 0;
  316. }
  317.  
  318. #undef atof
  319.  
  320. double mac_atof(const char * nptr)
  321. {
  322.     /* The Mac returns NaN(17) on undefined strings, which is not acceptable
  323.        for Perl
  324.     */
  325.     double res = atof(nptr);
  326.     
  327.     if (isnan(res) /* This doesn't work: && res == nan("17") */)
  328.         return 0.0;
  329.     else
  330.         return res;
  331. }
  332.  
  333. void reinit_missing()
  334. {
  335.     environ         =    nil;
  336.     pipes            =    nil;
  337. }
  338.  
  339.